﻿Imports QLTHUVIEN.BUS
Imports QLTHUVIEN.DTO
Public Class frmTraCuuSach

    Private Sub rdbMaSach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbMaSach.CheckedChanged
        If rdbMaSach.Checked Then
            txtMaSach.Enabled = True
            txtTenSach.Enabled = False
            txtNamXuatBan.Enabled = False
            cboTheLoai.Enabled = False
            cboNhaXuatBan.Enabled = False
        End If
    End Sub

    Private Sub rdbTenSach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbTenSach.CheckedChanged
        If rdbTenSach.Checked Then
            txtMaSach.Enabled = False
            txtTenSach.Enabled = True
            txtNamXuatBan.Enabled = False
            cboTheLoai.Enabled = False
            cboNhaXuatBan.Enabled = False
        End If
    End Sub

    Private Sub rdbNamXuatBan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbNamXuatBan.CheckedChanged
        If rdbNamXuatBan.Checked Then
            txtMaSach.Enabled = False
            txtTenSach.Enabled = False
            txtNamXuatBan.Enabled = True
            cboTheLoai.Enabled = False
            cboNhaXuatBan.Enabled = False
        End If
    End Sub

    Private Sub rdbTheLoai_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbTheLoai.CheckedChanged
        If rdbTheLoai.Checked Then
            txtMaSach.Enabled = False
            txtTenSach.Enabled = False
            txtNamXuatBan.Enabled = False
            cboTheLoai.Enabled = True
            cboNhaXuatBan.Enabled = False
        End If
    End Sub

    Private Sub rbNhaXuatBan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbNhaXuatBan.CheckedChanged
        If rdbNhaXuatBan.Checked Then
            txtMaSach.Enabled = False
            txtTenSach.Enabled = False
            txtNamXuatBan.Enabled = False
            cboTheLoai.Enabled = False
            cboNhaXuatBan.Enabled = True
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnTraCuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTraCuu.Click      
        If rdbMaSach.Checked = True Then
            Try
                'Kiem tra ma sach co hop le hay khong
                Dim masach As Integer
                If Integer.TryParse(txtMaSach.Text, masach) = False Then
                    MessageBox.Show("Mã sách không hợp lệ!")
                    txtMaSach.Focus()
                    Return
                End If

                Dim dt As DataTable
                Dim sachbus As New SachBUS()

                'Neu khong tim thay thi show messagebox
                dt = sachbus.TimKiemTheoMaSach(masach)
                If dt.Rows.Count = 0 Then
                    MessageBox.Show("Không tìm thấy!")
                Else
                    dtgDanhSachTraCuu.DataSource = dt
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If

        If rdbTenSach.Checked = True Then
            Try
                Dim tensach As String = txtTenSach.Text
                Dim dt As DataTable
                Dim sachbus As New SachBUS()
                dt = sachbus.TimKiemTheoTenSach(tensach)
                If dt.Rows.Count = 0 Then
                    MessageBox.Show("Không tìm thấy!")
                Else
                    dtgDanhSachTraCuu.DataSource = dt
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If

        If rdbNamXuatBan.Checked = True Then
            Try
                Dim namxuatban As Integer
                If Integer.TryParse(txtNamXuatBan.Text, namxuatban) = False Then
                    MessageBox.Show("Năm xuất bản không hợp lệ!")
                    txtNamXuatBan.Focus()
                    Return
                End If
                Dim dt As DataTable
                Dim sachbus As New SachBUS()
                dt = sachbus.TimKiemTheoNamXuatBan(namxuatban)
                If dt.Rows.Count = 0 Then
                    MessageBox.Show("Không tìm thấy!")
                Else
                    dtgDanhSachTraCuu.DataSource = dt
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If

        If rdbNhaXuatBan.Checked = True Then
            Try
                Dim nhaxuatban As String = cboNhaXuatBan.SelectedItem.ToString()
                Dim dt As DataTable
                Dim sachbus As New SachBUS()
                dt = sachbus.TimKiemTheoNhaXuatBan(nhaxuatban)
                If dt.Rows.Count = 0 Then
                    MessageBox.Show("Không tìm thấy!")
                Else
                    dtgDanhSachTraCuu.DataSource = dt
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If

        If rdbTheLoai.Checked = True Then
            Try
                Dim theloai As String = cboTheLoai.SelectedItem.ToString()
                Dim dt As DataTable
                Dim sachbus As New SachBUS()
                dt = sachbus.TimKiemTheoTheLoai(theloai)
                If dt.Rows.Count = 0 Then
                    MessageBox.Show("Không tìm thấy!")
                Else
                    dtgDanhSachTraCuu.DataSource = dt
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub frmTraCuuSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim dt As DataTable
            Dim theloaibus As New TheLoaiSachBUS()
            dt = theloaibus.layDanhSach()
            For i As Integer = 0 To dt.Rows.Count - 1
                cboTheLoai.Items.Add(dt(i)(1))
            Next

            Dim nhaxuatbanbus As New NhaXuatBanBUS()
            dt = nhaxuatbanbus.LayDanhSach()
            For i = 0 To dt.Rows.Count - 1
                cboNhaXuatBan.Items.Add(dt(i)(1))
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class